import { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';

/**
 * 将子组件挂载在 body 下
 */
const Portal = ({ children }) => {
    const [el] = useState(document.createElement('div'));
    useEffect(() => {
        document.body.appendChild(el);
        return () => {
            document.body.removeChild(el);
        };
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    return createPortal(children, el);
};

export default Portal;
